package com.dmxh.controller;

import com.alibaba.fastjson.JSON;
import com.dmxh.pojo.TbAdmin;
import com.dmxh.pojo.TbSeller;
import com.dmxh.pojo.TbUser;
import com.dmxh.result.Result;
import com.dmxh.result.ResultConstant;
import com.dmxh.token.LoginUsernamePasswordToken;
import com.dmxh.util.JwtUtil;
import com.dmxh.vo.UserVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @PostMapping("/adminLogin")
    public Result adminLogin(@RequestBody Map<String,String>map) {
        String username = map.get("username");
        String password = map.get("password");
        LoginUsernamePasswordToken usernamePasswordToken = new LoginUsernamePasswordToken(username, password,"admin");
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(usernamePasswordToken);
        } catch (UnknownAccountException | IncorrectCredentialsException e) {
            return Result.response(ResultConstant.ERROR_LOGIN);
        }
        TbAdmin principal = (TbAdmin) subject.getPrincipal();
        String adminToken = JwtUtil.createJwt(principal.getId().toString(), JSON.toJSONString(principal), "adminToken");
        return new Result(ResultConstant.SUCCESS_LOGIN.getCode(), ResultConstant.SUCCESS_LOGIN.getMessage()).setData(adminToken);
    }
    @PostMapping("/sellerLogin")
    public Result sellerLogin(@RequestBody Map<String,String>map) {
        String username = map.get("username");
        String password = map.get("password");
        LoginUsernamePasswordToken usernamePasswordToken = new LoginUsernamePasswordToken(username, password,"seller");
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(usernamePasswordToken);
        } catch (UnknownAccountException | IncorrectCredentialsException e) {
            return Result.response(ResultConstant.ERROR_LOGIN);
        }
        TbSeller principal = (TbSeller) subject.getPrincipal();
        String sellerToken = JwtUtil.createJwt(principal.getSellerId(), JSON.toJSONString(principal), "sellerToken");
        HashMap<String, String> map1 = new HashMap<>();
        map1.put("token", sellerToken);
        map1.put("username", principal.getSellerId());
        return new Result(ResultConstant.SUCCESS_LOGIN.getCode(), ResultConstant.SUCCESS_LOGIN.getMessage()).setData(map1);
    }

    @PostMapping("/userLogin")
    public Result userLogin(@RequestBody UserVo userVo) {
        LoginUsernamePasswordToken loginUsernamePasswordToken = new LoginUsernamePasswordToken(userVo.getUsername(),userVo.getPassword(),"user");
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(loginUsernamePasswordToken);
        } catch (UnknownAccountException e) {
            return Result.response(ResultConstant.ERROR_LOGIN);
        } catch (IncorrectCredentialsException e) {
           return Result.response(ResultConstant.ERROR_LOGIN);
        }
        TbUser user = (TbUser) subject.getPrincipal();
        String userToken = JwtUtil.createJwt(user.getId().toString(), JSON.toJSONString(user), "userToken");
        return Result.response(ResultConstant.SUCCESS_LOGIN).setData(userToken);
    }

}
